/**
 * http://surenpi.com
 */
package autotest.web.framework.plugin.wizards;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * 填充属性的向导页面
 * @author suren
 * @date Jul 23, 2016 9:23:34 AM
 */
public class NewPageWizardFieldPage extends WizardPage
{
	private List<PageFieldUI> pageFieldUIList = new ArrayList<PageFieldUI>();
	private Map<String, String> pageFieldMap = new HashMap<String, String>();
	
	private Validator validator;
	
	/*
	 * @param pageName
	 */
	protected NewPageWizardFieldPage(String pageName)
	{
		super(pageName);
	}

	@Override
	public void createControl(Composite parent)
	{
		Composite container = new Composite(parent, SWT.NULL);
		container.setLayout(new FillLayout());
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		
		validator = new Validator();
		
		createFieldGrid(container);
		
		setControl(container);
	}

	/**
	 * @param container
	 */
	private void createFieldGrid(Composite container)
	{
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		
		ScrolledComposite scrolledComposite = new ScrolledComposite(
				container, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		final Composite fieldComposite = new Composite(scrolledComposite, SWT.NULL);
		scrolledComposite.setContent(fieldComposite);
		fieldComposite.setLayout(gridLayout);
		fieldComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		//按钮
		Composite actionCom = new Composite(fieldComposite, SWT.NULL);
		actionCom.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, gridLayout.numColumns, 1));
		RowLayout actionLayout = new RowLayout();
		actionCom.setLayout(actionLayout);
		Button addBut = new Button(actionCom, SWT.PUSH);
		addBut.setText("add");
		addBut.addMouseListener(new MouseAdapter()
		{

			@Override
			public void mouseUp(MouseEvent e)
			{
				PageFieldUI pageFieldUI = new PageFieldUI();
				pageFieldUI.create(fieldComposite);
				pageFieldUIList.add(pageFieldUI);
				
				pageFieldUI.addObserver(validator);
				
				setPageComplete(false);
				fieldComposite.layout(true, true);
			}
			
		});
		
		//添加表头
		Label label = new Label(fieldComposite, SWT.NULL);
		label.setText("type");
		label = new Label(fieldComposite, SWT.NULL);
		label.setText("name");
		
		scrolledComposite.setMinSize(fieldComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}

	public List<PageFieldUI> getPageFieldUIList()
	{
		return pageFieldUIList;
	}

	/**
	 * 属性UI
	 * @author zhaoxj
	 * @since jdk1.6
	 * 2016年8月31日
	 */
	class PageFieldUI extends Observable
	{
		public static final int TYPE = 0X1;
		public static final int NAME = 0X2;
		
		private Composite parent;
		
		private Combo typeCombo;
		private Text nameText;
		
		public void create(Composite parent)
		{
			this.parent = parent;

			//create ui
			typeCombo = new Combo(parent, SWT.NULL | SWT.READ_ONLY);
			typeCombo.setData(TYPE);
			
			nameText = new Text(parent, SWT.NULL);
			nameText.setData(NAME);
			
			//add event
			addModifyListener();
			
			//init data
			typeCombo.add("Text");
			typeCombo.add("Button");
			typeCombo.add("Selector");
			typeCombo.add("FileUpload");
			typeCombo.add("CheckBoxGroup");
			typeCombo.select(0);
		}
		
		public void dispose()
		{
			typeCombo.dispose();
			nameText.dispose();
			
			parent.layout(true, true);
		}
		
		private void addModifyListener()
		{
			typeCombo.addModifyListener(new ModifyListener()
			{
				
				@Override
				public void modifyText(ModifyEvent e)
				{
					setChanged();
					notifyObservers(e.getSource());
				}
			});
			
			nameText.addModifyListener(new ModifyListener()
			{
				
				@Override
				public void modifyText(ModifyEvent e)
				{
					setChanged();
					notifyObservers(e.getSource());
				}
			});
		}
		
		public boolean isValid()
		{
			return !"".equals(getNameField());
		}
		
		public String getNameField()
		{
			return nameText.getText();
		}
		
		public String getType()
		{
			int index = typeCombo.getSelectionIndex();
			if(index != -1)
			{
				return typeCombo.getItem(index);
			}
			else
			{
				return null;
			}
		}
	}
	
	class Validator implements Observer
	{

		@Override
		public void update(Observable o, Object arg)
		{
			setPageComplete(isPageFieldListValid());
		}
	}
	
	private boolean isPageFieldListValid()
	{
		pageFieldMap.clear();
		for(PageFieldUI pageFieldUI : pageFieldUIList)
		{
			if(!pageFieldUI.isValid())
			{
				return false;
			}
			
			String nameField = pageFieldUI.getNameField();
			if(!pageFieldMap.containsKey(nameField))
			{
				pageFieldMap.put(nameField, pageFieldUI.getType());
			}
		}
		
		return true;
	}

	public Map<String, String> getPageFieldMap()
	{
		return pageFieldMap;
	}
}
